package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.HomeMarketInitDataBeen;
import cn.xfdzx.android.apps.shop.bean.SelfOwnedShopBeen;

/**
 * @author gzk
 * @date 2020/3/28 0028
 * description：自营4个图片
 */
public class MarketZiyingGVAdapter extends BaseAdapter {

    private Context mContext;
    private List<SelfOwnedShopBeen.DataBean.GoodsBean> mList = new ArrayList<>();
    private ImageView ivHomeZiying;
    private TextView tvZiyingPrice;
    private TextView tvZiyingOldPrice;
    private TextView tv_name;

    public MarketZiyingGVAdapter(Context mContext) {
        this.mContext = mContext;

    }

    public void setData(List<SelfOwnedShopBeen.DataBean.GoodsBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {

        if (mList.size() >= 3) {

            return 3;
        } else {
            return mList.size();

        }

    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        view = View.inflate(mContext, R.layout.item_home_ziying, null);
        ivHomeZiying = view.findViewById(R.id.iv_home_ziying);
        tvZiyingPrice = view.findViewById(R.id.tv_ziying_price);
        tvZiyingOldPrice = view.findViewById(R.id.tv_ziying_old_price);
        tv_name = view.findViewById(R.id.tv_name);
        RoundedCorners roundedCorners = new RoundedCorners(10);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        options.placeholder(R.drawable.icon_cover)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_cover) //url为空的时候,显示的图片
                .error(R.drawable.icon_cover);
        Glide.with(mContext).load(mList.get(position).getThumb()).apply(options.transform(new CenterCrop(), roundedCorners)).into(ivHomeZiying);

        if (Double.parseDouble(mList.get(position).getDiscountprice()) == 0) {
            tvZiyingPrice.setText("¥" + mList.get(position).getMarketprice()+"/"+mList.get(position).getUnit());
            tvZiyingOldPrice.setVisibility(View.GONE);

        } else {
            tvZiyingOldPrice.setVisibility(View.GONE);
            tvZiyingPrice.setText("¥" + mList.get(position).getDiscountprice()+"/"+mList.get(position).getUnit());
            tvZiyingOldPrice.setText("¥" + mList.get(position).getMarketprice());
        }
        tvZiyingOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

        tv_name.setText(mList.get(position).getTitle());
        return view;
    }
}
